﻿#include <bits/stdc++.h>
#include <iostream>
typedef int ElemType;

struct FLNode {
    ElemType data;
    int next;
};

const int TABLE_SIZE = 100;
FLNode hashTable[TABLE_SIZE];

void initHashTable() {
    for (int i = 0; i < TABLE_SIZE; ++i) {
        hashTable[i].data = 0;
        hashTable[i].next = -1;
    }
}

int hashFunction(ElemType key) {
    return key % TABLE_SIZE;
}

void insertHashTable(ElemType elem) {
    int index = hashFunction(elem);
    while (hashTable[index].data != 0) {
        index = (index + 1) % TABLE_SIZE;
    }
    hashTable[index].data = elem;
}

bool removeHashTable(ElemType elem) {
    int index = hashFunction(elem);
    int prevIndex = -1;
    while (hashTable[index].data != elem) {
        prevIndex = index;
        index = (index + 1) % TABLE_SIZE;
        if (prevIndex == index) return false; //元素未找到
    }
    if (prevIndex == -1) {
        hashTable[index].data = 0;
    }
    else {
        int nextIndex = hashTable[index].next;
        hashTable[prevIndex].next = nextIndex;
        hashTable[index].data = 0;
    }
    return true;
}

ElemType searchHashTable(ElemType elem) {
    int index = hashFunction(elem);
    while (hashTable[index].data != 0) {
        if (hashTable[index].data == elem) {
            return elem;
        }
        index = (index + 1) % TABLE_SIZE;
    }
    return -1; // 元素未找到
}

int main() {
    std::ifstream inputFile("hash_table_input.txt");
    if (!inputFile.is_open()) {
        std::cerr << "无法打开文件" << std::endl;
        return 1;
    }

    initHashTable();

    std::string operation;
    ElemType elem;
    while (inputFile >> operation) {
        if (operation == "insert") {
            inputFile >> elem;
            insertHashTable(elem);
            std::cout << elem << " 成功插入" << std::endl;
        }
        else if (operation == "remove") {
            inputFile >> elem;
            if (removeHashTable(elem)) {
                std::cout << elem << " 已经移除" << std::endl;
            }
            else {
                std::cout << elem << " 未找到" << std::endl;
            }
        }
        else if (operation == "search") {
            inputFile >> elem;
            if (searchHashTable(elem) != -1) {
                std::cout << elem << " 成功找到" << std::endl;
            }
            else {
                std::cout << elem << "未找到" << std::endl;
            }
        }
    }

    inputFile.close();
    return 0;
}